import com.earldouglas.xsbtwebplugin.WebPlugin
import sbt.Keys._
import sbt._

object BuildSettings {
  import com.earldouglas.xsbtwebplugin.PluginKeys._

  System.setProperty("run.mode", "production")

  val dist = TaskKey[Unit]("dist")

  val buildSettings: Seq[Setting[_]] = Defaults.defaultSettings ++ Seq[Setting[_]](
    organization := "im.yagni",
    version := "0.0.1-SNAPSHOT",
    scalaVersion := "2.10.1",
    scalacOptions ++= Seq("-deprecation", "-optimise", "-explaintypes"),
//    crossScalaVersions := Seq(/*"2.9.2", */"2.10.1"),
    javaOptions ++= Seq("-Xmx256m", "-Xss4m", "-server"),
  
    parallelExecution := true,
    parallelExecution in Test := true,
    logBuffered := false,

//    testOptions in Test += Tests.Cleanup( (loader: java.lang.ClassLoader) => {
//      loader.loadClass("im.yagni.acceptance.Stop").newInstance()
//    }),

//    testOptions in Test ++= Seq(Tests.Argument("junitxml", "html", "console")),

    dist <<= Seq(packageWar in Compile).dependOn)
}

object Resolvers {
  val sonatypeRepo = "Sonatype Repo" at "http://oss.sonatype.org/content/groups/public/"
  val timt = "Tim Tennant's repo" at "http://dl.bintray.com/timt/repo/"
}

object Dependencies {
  private val driveByVersion = "0.2.22"
  private val liftVersion	= "2.5"
  private val jettyVersion = "8.1.14.v20131031"
  private val logbackVersion = "1.0.13"
  private val naiveHttpVersion = "48"

  val singles = Seq(
    "im.yagni" %% "driveby" % driveByVersion % "test",
    "org.eclipse.jetty" % "jetty-webapp" % jettyVersion % "container",
    "ch.qos.logback" % "logback-classic" % logbackVersion,
    "io.shaka" %% "naive-http" % naiveHttpVersion,
    "im.mange" %% "jetboot" % "0.0.4"
  )
  
  val lift = Seq(
    "net.liftweb" %% "lift-webkit" % liftVersion
  )

  val akka = Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.3.5" //,
//    "com.typesafe.akka" %% "akka-remote" % akkaVersion,
//    "com.typesafe.akka" %% "akka-slf4j" % akkaVersion
  )
}

/* see http://www.scala-sbt.org/using_sonatype.html and http://www.cakesolutions.net/teamblogs/2012/01/28/publishing-sbt-projects-to-nexus/
 * Instructions from sonatype: https://issues.sonatype.org/browse/OSSRH-2841?focusedCommentId=150049#comment-150049
 * Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
 * Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
 * Promote staged artifacts into repository 'Releases'
 * Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public
 * Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging
 */
object Publishing {

  def publishSettings: Seq[Setting[_]] = Seq(
    publishMavenStyle := true,
    publishArtifact in Test := false,
    pomIncludeRepository := { x ⇒ false },

    publishTo <<= version { v: String ⇒
      val nexus = "https://oss.sonatype.org/"
      if (v.trim.endsWith("SNAPSHOT"))
        Some("snapshots" at nexus + "content/repositories/snapshots")
      else
        Some("releases" at nexus + "service/local/staging/deploy/maven2")
    },

    pomExtra := (
      <licenses>
        <license>
          <name>MIT License</name>
          <url>http://www.opensource.org/licenses/mit-license/</url>
          <distribution>repo</distribution>
        </license>
      </licenses>
      <scm>
        <url>http://shoreditch.googlecode.com/svn/trunk/</url>
      </scm>
      <url>http://code.google.com/p/shoreditch/</url>
      <developers>
        <developer>
          <id>alltonp</id>
          <name>Paul Allton</name>
          <email>paulallton [at] gmail [dot] com</email>
          <organization>Flatmap Ltd</organization>
        </developer>
      </developers>))
}

object SbtBuild extends Build {
  import BuildSettings._
  import Dependencies._
  import Publishing._
  import Resolvers._

  lazy val app = Project("Shoreditch", file("."),
    settings = buildSettings ++ WebPlugin.webSettings ++ publishSettings ++ Seq(resolvers := Seq(sonatypeRepo, timt, Classpaths.typesafeResolver),
      libraryDependencies ++= lift ++ akka ++ singles))
}

